package com.hankol.winecellar

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.activity.BaseActivity
import com.baselibrary.extension.checkNotNull
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.hankol.winecellar.adapter.AllEvaluateAdapter
import com.hankol.winecellar.bean.EvaluateListBean
import com.hankol.winecellar.databinding.ActivityEvaluateAllListBinding
import com.baselibrary.utils.EmptyViewUtil
import com.hankol.winecellar.viewmodel.WineCellarViewModel

/**全部评价
 * @author: merbng
 * @time: 2022/6/16 19:03
 * @description:
 */
@Route(path = ActivityRouter.WINE_CELLAR.ALL_EVALUATE)
class WineEvaluateAllActivity : BaseActivity() {

    private lateinit var viewModel: WineCellarViewModel
    private lateinit var binding: ActivityEvaluateAllListBinding
    private var mPageNum = 1
    private var wineryCode = ""
    val adapter: AllEvaluateAdapter by lazy { AllEvaluateAdapter() }
    private var hasnext: Boolean = false

    override fun getLayoutId(): Int {
        return R.layout.activity_evaluate_all_list
    }

    override fun init() {
        if (intent.extras.checkNotNull()) {
            if (ARouterUtils.parseParams(intent.extras!!, "wineryCode").size > 0) {
                if (ARouterUtils.parseParams(intent.extras!!, "wineryCode")[0].checkNotNull()) {
                    wineryCode = ARouterUtils.parseParams(intent.extras!!, "wineryCode")[0]
                }
            }
        }
        showLoadingDialog()
        binding = getDataBinding()
        viewModel = getActivityViewModelProvider(this).get(WineCellarViewModel::class.java)
        binding.ivBack.setOnClickListener { finish() }
        binding.rvList.setLayoutManager(LinearLayoutManager(this))
        binding.rvList.adapter = adapter
        adapter.setOnItemChildClickListener { adapter, view, position ->
            val bean = (adapter.getItem(position) as EvaluateListBean)

        }
        binding.smartRefresh.setOnRefreshListener {
            mPageNum = 1
            loadData()
        }
        binding.smartRefresh.setOnLoadMoreListener {
            if (hasnext) {
                loadData()
            } else {
                binding.smartRefresh.finishLoadMore()
            }
        }

        viewModel.evaluateListLD.observe(this, Observer {
            hideLoadingDialog()
            binding.smartRefresh.finishRefresh()
            if (it == null) {
                adapter.emptyView = EmptyViewUtil.getEmptyView(Constants.TYPE_FOLLOW_FANS_FRIEND, binding.rvList, "暂无评价")
                return@Observer
            }
            if (it.data?.size == 0 && mPageNum == 1) {//首次为空
                adapter.emptyView = EmptyViewUtil.getEmptyView(Constants.TYPE_FOLLOW_FANS_FRIEND, binding.rvList, "暂无评价")
            } else {
                if (mPageNum == 1) {
                    adapter.setNewData(it.data)
                } else {
                    adapter.addData(it.data!!)
                    binding.smartRefresh.finishLoadMore()
                }
                if (it.data.checkNotNull()) {
                    hasnext = it.totalCount != adapter.data.size
                }
                mPageNum++
            }
        })
        loadData()
    }

    fun loadData() {
        viewModel.requestEvaluateList(wineryCode, mPageNum)
    }
}